<?php

include_once "includeFiles.php";

if($_POST['action'] == "buy_pay"){

	$yancode = $_POST['yancode'];
	if(empty($_SESSION['seccode'])|| trim($yancode) != $_SESSION['seccode']){
		$objBase->popup("验证码输入有误，请重新输入！");
	}
	
	//查询购物车
	$sql = "select * from hy_b_car where memberID='".$_SESSION['user_id']."' ";
	$objDb->query($sql);
	$row = $objDb->get_data();
	
	if(is_array($row)){
	
	    //查询积分够不够 
	    $sql="select zhifen from hy_member where user_id='".$_SESSION["user_id"]."'";
		$objDb->query($sql);
		$daterow=$objDb->get_data();
		$totalzhifen = $daterow[0]['zhifen'];
		$haixuzhifen = ($_POST['totalzhifen']) - $totalzhifen;
		if($totalzhifen < $_POST['totalzhifen']){
	       $objBase->popup("您现在的积分为：".$totalzhifen."点积分，您购买的产品需要". $_POST['totalzhifen']."点积分，还需".$haixuzhifen."点积分，请您及时充值！  ",'dzf.php');
		   exit();
		}
		
		
		//产生订单号
		$sql = "select (max(id)+1) as maxid from hy_b_car ";
		$objDb->query($sql);
		$orderNumdDta = $objDb->get_data();
		if($orderNumdDta){ 
		   $maxid       = $orderNumdDta[0]["maxid"]; 
		   $strordernum = date("YmdHis",time()).$maxid; //产生订单号
		}
	
	    $strallmoney = 0;     //总价
		$strpid      = "";    //所有产品ID封装
		$strpnum     = "";    //所有产品数量封装
		$strpname    = "";    //所有产品名称封装
		$strpprice   = "";    //所有产品价格封装
		
		foreach($row as $rs){
			
			$strallmoney  += $rs["money"];
			$strordernums  = $strordernum;
			
			if(!$rs["productID"])   {$strpid     = $rs["productID"];    } else { $strpid     .= ",".$rs["productID"];    }  //ID封装
			if(!$rs["buyNum"])      {$strpnum    = $rs["buyNum"];       } else { $strpnum    .= ",".$rs["buyNum"];       }  //数量封装
			if(!$rs["productName"]) {$strpname   = $rs["productName"];  } else { $strpname   .= ",".$rs["productName"];  }  //产品名封装
			if(!$rs["price"])       {$strpprice  = $rs["price"];        } else { $strpprice  .= ",".$rs["price"];        }  //价格封装
			if(!$rs["H_TotalPrice"]){$TotalPrice = $rs["H_TotalPrice"]; } else { $TotalPrice .= ",".$rs["H_TotalPrice"]; }  //总价封装
		} 

		//产生新的订单
		$arr = array(
			"orderNum"    => "$strordernums",        //订单号
			"memberID"    => $_SESSION['user_id'],   //会员ID
			"productName" => $strpname, //产品名
			"productID"   => "$strpid",              //产品ID
			"buyNum"      => "$strpnum",             //产品数量
			"price"       => "$strpprice",           //产品价格
			"Totalprice"  => $strallmoney,         //总价
			"createdate"  =>  time(),                //创建时间
		    "state" =>2,
		);
		
		$arr = array_merge($arr,$_POST);
		
		
		$arr = unsetArray($arr,array('action','yancode'));
		//去掉不想要的元素
		
		$strSql = $hb->insert('hy_b_order',$arr);
		
		$objDb->query($strSql);
		$blnReSult = $objDb->get_data();
		
		if(!$blnReSult){
			if($_POST['PayType'] == "122"){
			 
			} else {
			    $yuzhifen = $totalzhifen - $_POST['totalzhifen'];
			    
				//删除购物车商品
				$sql = "delete from hy_b_car where memberID='".$_SESSION['user_id']."'";
				$objDb->query($sql);
				$fltPrice = $_POST['ProductMoney'] + $_POST['SendTypeMoney'];
				
				$arr = array(
				   'oid'   => $strordernums,
				   'st'    => $_POST['SendType'],
				   'pt'    => $_POST['PayType'],
				   'price' => $strallmoney,
				);
				
				//扣除积分
				 $sql="update hy_member set `zhifen` = (`zhifen`-".$strallmoney.") where user_id='".$_SESSION["user_id"]."'";
				 $objDb->query($sql);
				
				$content = date("Y年m月d日Y:h:i",time()).",".$_SESSION['user_name'].'购买'.$strpname.'扣减'.$strallmoney.'点积分';
	    		$arr = array(   
	    		            "name"       => $_SESSION['user_name'],
						    "content"    => $content,
						    "type"       => 1,
						    "createtime" => time(),
						);
			
		    	$mpoint->saveLog($arr);
		    	
		    	$str = "您好！".$_SESSION["user_name"]."，您的订单已提交成功，扣除".$_POST['totalzhifen']."点积分，剩余".$yuzhifen."点积分，祝愿您购物愉快！）";
				$objBase->popup($str,"account.php");
			}
		} else {
			$objBase->popup("支付跳转失败，请重试","account.php");
		}
	} else {
		$objBase->popup("购物车中没有产品","account.php");
	}
} else { 
    $objBase->popup("下单失败，请重试");
}	


 function unsetArray($array,$delarr){
		foreach ($array as $bkey=>$bvalue){
			foreach ($delarr as $key=>$value){
				if($value == $bkey){
				unset($array[$bkey]);		
			    }
		     }
	    }
		return $array;
	}

?>
